package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: bowen
 * @Date: 2019/7/22 16:57
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController{

	@RequestMapping("/getUsername")
	public Result getUsername() {
		try {
			User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,principal.getUsername());
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false, MessageConstant.GET_USERNAME_FAIL);
		}
	}
}
